<?php

class CoreController extends IzwebzCore {

	function __construct()
	{
		parent::__construct();
		// $this->registerScript('jquery_min_lib', CORE_JS_URL.'jquery.min.js',array('jquery'));
		// $this->registerScript('jquery_min_lib','https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js',array('jquery'));
		$this->registerScript('jquery-ui-1.8.1',CORE_JS_URL.'jquery-ui-1.10.4.custom.min.js',array('jquery'));
		if(!is_admin()){
			$this->registerScript('facebook_dev','https://connect.facebook.net/en_US/all.js',array('jquery'));
			// $this->registerScript('bootstrap-js',CORE_LIBRARY.'bootstrap/js/bootstrap.min.js',array('jquery'));
			// wp_enqueue_style('bootstrap-css',CORE_LIBRARY.'bootstrap/css/bootstrap.min.css');
		}
		$this->registerScript('core_js', CORE_JS_URL.'core.js',array('jquery'));
		$this->registerScript('jquery_me', CORE_JS_URL.'jquery.js',array('jquery'));
		wp_enqueue_style('jquery-ui-css',CORE_CSS_URL.'ui-lightness/jquery-ui.css');
		wp_enqueue_style('core_style',CORE_CSS_URL.'core.css');
		wp_localize_script('core_js','ajax_script', array(
            'ajax_image' => CORE_IMAGE_URL,
            'ajaxurl' => admin_url('admin-ajax.php')
        ));
	}

	public function getTheme()
	{
		return get_template();
	}

	public function getClass()
	{
		$class = get_class($this);
		$class = strtolower($class);
		$class = str_replace('controller', '', $class);
		return '/'.$class.'/';
	}

	public function render($view,$arrVariables = array(),$dir = null)
	{
		$fileView = CORE_DIR.'views/'.$this->getTheme().$this->getClass().$view.'.php';
		if($dir !== null) {
			$fileView = CORE_DIR.'views/'.$this->getTheme().$dir.'.php';
		}
		if(is_file($fileView) && file_exists($fileView)){
			if(!empty($arrVariables)) {
				extract($arrVariables);
			}
			require_once($fileView);
		}else{
			die('File not found : '.$fileView);
		}
	}

	public function renderPartial($view,$arrVariables = array(),$dir = null){
		$fileView = CORE_DIR.'views/'.$this->getTheme().$this->getClass().$view.'.php';
		if($dir !== null) {
			$fileView = CORE_DIR.'views/'.$this->getTheme().$dir.'.php';
		}
		if(is_file($fileView) && file_exists($fileView)){
			if(!empty($arrVariables)) {
				extract($arrVariables);
			}
			require($fileView);
		}else{
			die('File not found : '.$fileView);
		}
	}

	public function element($view,$arrVariables = array(),$dir = null)
	{
		$fileElement = CORE_DIR.'views/'.$this->getTheme().'/elements/'.$view.'.php';
		if($dir !== null) {
			$fileElement = CORE_DIR.'views/'.$this->getTheme().$dir.'.php';
		}
		if(is_file($fileElement) && file_exists($fileElement)){
			if(!empty($arrVariables)) {
				extract($arrVariables);
			}
			require($fileElement);
		}else{
			die('File not found : '.$fileElement);
		}
	}

	public function registerScript($name, $path, $dependencies = array(), $version = null, $in_footer = false) {
        if (!wp_script_is($name, 'registered')) {
            wp_register_script($name, $path, $dependencies, $version, $in_footer);
        }
        if (!wp_script_is($name, 'queue')) {
            wp_enqueue_script($name);
        }
    }
}